package org.jogdiary2011.service;

import java.util.List;
import java.util.Map;

import org.jogdiary2011.meta.ExerciseResultMeta;
import org.jogdiary2011.model.ExerciseResult;
import org.jogdiary2011.model.Person;
import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;


public class ExerciseResultService {

    private PersonService personService = new PersonService();
    private ExerciseResultMeta exerciseResultMeta = new ExerciseResultMeta();
    
    public ExerciseResult insert(Map<String, Object> requestMap) {
        // TODO Auto-generated method stub
        ExerciseResult model = new ExerciseResult();
        BeanUtil.copy(requestMap, model);
        
        // get person
        Person person = personService.getByNickName(requestMap.get("person").toString());
        model.getPersonRef().setModel(person);
        
        Datastore.put(model);
        System.out.println(model.getKey());
        System.out.println(model.getDescription());
        return model;
    }

    public List<ExerciseResult> findAll() {
        return Datastore.query(exerciseResultMeta).sort(exerciseResultMeta.exerciseDate.desc).asList();
    }
}
